package com.yumeng.framework.auth.security.authentication;

import com.yumeng.common.api.ApiCode;
import com.yumeng.common.utils.ResponseUtils;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;

import java.io.IOException;

/**
 * 认证失败处理器（JSON）（用于前后端分离）
 *
 * @author wxd
 * @date 2024/10/9 14:48
 */
public class JsonAuthenticationFailureHandler implements AuthenticationFailureHandler {

    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        ResponseUtils.writeJson(response, ApiCode.LOGIN_FAIL, String.format("登录失败：%s", exception.getMessage()));
    }
}
